﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MTQ.Common
{
    /// <summary>
    /// 存放公用的方法
    /// </summary>
    public static class ExtendMethod
    {
        public static Double AddDoubleData(Double num1, Double num2, Int32 count)
        {
            string result = (num1 + num2).ToString("N" + count.ToString());
            return Convert.ToDouble(result);
        }

        public static int[] ToInts(this string[] arrStr1)
        {

            int[] arrInt = new int[] { };
            if (arrStr1.Length == 0)
            {
                return arrInt;
            }
            try
            {
                arrInt = Array.ConvertAll<string, int>(arrStr1, delegate (string s) { return int.Parse(s); });
            }
            catch
            {
            }
            return arrInt;
        }

        public static List<Guid> ToListGuid(this string[] arrStr1)
        {

            List<Guid> list = new List<Guid>();
            if (arrStr1.Length == 0)
            {
                return list;
            }
            try
            {
                foreach (var item in arrStr1.ToList())
                {

                    list.Add(Guid.Parse(item));
                }
            }
            catch
            {
            }
            return list;
        }
        public static List<int> ToListInt(this string[] arrStr1)
        {

            List<int> list = new List<int>();
            if (arrStr1.Length == 0)
            {
                return list;
            }
            try
            {
                foreach (var item in arrStr1.ToList())
                {

                    list.Add(int.Parse(item));
                }
            }
            catch
            {
            }
            return list;
        }

        public static string _Substring(this string arrStr, int startIndex, int endIndex)
        {
            if (arrStr.Length <= endIndex)
            {
                return arrStr;
            }
            return arrStr.Substring(startIndex, endIndex);
        }


        //public static string _CollBack(string state)
        //{
        //    if (state == "s") { 
        //    return _CollBack('<span class="remindSign">&nbsp;F&nbsp;</span>');
        //    }
        //    return _CollBack(state);
        //}

        public static bool _Contains(this List<int> listInts, int? num)
        {
            if (num == null)
            {
                return false;
            }

            return listInts.Contains(Convert.ToInt16(num));
        }

        public static string _ToString(this DateTime? datatime, string format)
        {
            if (datatime == null)
            {
                return null;
            }

            return Convert.ToDateTime(datatime).ToString(format);
        }


        public static List<int?> _ToListInt(this List<int> listInt)
        {
            List<int?> list = new List<int?>();
            foreach (var item in listInt)
            {
                list.Add(item);
            }
            return list;
        }

        public static string _GetRegexString(this string logtxt, string regexstr)
        {
            Regex rgAddressArea = new Regex(regexstr, RegexOptions.Multiline | RegexOptions.Singleline);
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex("[ ]{2,}", options);
            logtxt = regex.Replace(logtxt, "");

            return rgAddressArea.Match(logtxt).Value;
        }

        public static string _GetTimeout(this string logtxt, string name)
        {
            string[] striparr = logtxt.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < striparr.Length; i++)
            {
                if (striparr[i].IndexOf(name) > -1)
                {

                    string str = striparr[i].Replace(name, "").Replace("REG_SZ", "");
                    return str;
                }
            }
            return "";
        }

        public static int? _ToInt(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch
            {
                return null;
            }

        }
        public static double? _ToDouble(this string str)
        {
            try
            {
                return Convert.ToDouble(str);
            }
            catch
            {
                return null;
            }

        }

        public static DateTime? _ToDatetime(this string str)
        {
            try
            {
                return Convert.ToDateTime(str);
            }
            catch
            {
                return null;
            }

        }
       

        public static int _ToInt2(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch
            {
                return 0;
            }

        }
    }
}
